package cn.wolfcode.shop.web.controller;

import cn.wolfcode.shop.query.ProductQueryObject;
import cn.wolfcode.shop.service.IBrandService;
import cn.wolfcode.shop.service.ICatalogService;
import cn.wolfcode.shop.service.IProductService;
import cn.wolfcode.shop.service.IPropertyService;
import cn.wolfcode.shop.util.UploadUtil;
import cn.wolfcode.shop.utils.PageResult;
import cn.wolfcode.shop.vo.JsonResultVO;
import cn.wolfcode.shop.vo.ProductVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.sun.deploy.panel.IProperty;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Controller
public class ProductController {
    @Reference
    private IProductService productService;
    @Reference
    private ICatalogService catalogService;
    @Reference
    private IBrandService brandService;
    @Reference
    private IPropertyService propertyService;
    @Value("${file.path}")
    private String url;

    @RequestMapping("product")
    public String product(){
        return "product/product";
    }

    /**
     *商品列表
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("product/page")
    public String productPage(@ModelAttribute("qo")ProductQueryObject qo, Model model){
        PageResult pageResult =productService.selectForPage(qo);
        model.addAttribute("pageResult",pageResult);
        return "product/product_list";
    }

    /**
     * 商品录入
     * @param model
     * @return
     */
    @RequestMapping("product/add")
    public String productAdd( Model model){
        model.addAttribute("brandList",brandService.selectAll());
        model.addAttribute("catalogList",catalogService.selectAll());

        return "product/product_add";
    }

    /**
     * 上传图片
     * @return
     */
    @RequestMapping("/product/uploadImage")
    @ResponseBody
    public String uploadImage(MultipartFile img){
        String fileName = UploadUtil.upload(img, url);
        return fileName;
    }
    /**
     *
     * @param upload ckedit默认找upload变量名
     * @param request ckedit上传图片需要用到
     * @param response ckedit上传图片需要用到
     */
    @RequestMapping("/product/ckeditUploadImg")
    public void ckeditUploadImg(MultipartFile upload, HttpServletRequest request, HttpServletResponse response){
        try {
            //上传文件并返回文件名
            String fileName = UploadUtil.upload(upload, url);
            // 结合ckeditor功能
            // imageContextPath为图片在服务器地址，如/upload/123.jpg,非绝对路径
            String imageContextPath =fileName;
            response.setContentType("text/html;charset=UTF-8");
            String callback = request.getParameter("CKEditorFuncNum");
            PrintWriter out = response.getWriter();
            out.println("<script type=\"text/javascript\">");
            out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + imageContextPath + "',''" + ")");
            out.println("</script>");
            out.flush();
            out.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 商品属性
     * @param catalogId
     * @param model
     * @return
     */
    @RequestMapping("product/getPropertyValueTr")
    public String getPropertyValueTr(Long catalogId,Model model){
        model.addAttribute("productPropertyList",propertyService.selectByCatalogId(catalogId));
        return "product/product_property_list";
    }

    /**
     * 商品录入保存
     * @return
     */
    @RequestMapping("product/save")
    @ResponseBody
    public JsonResultVO save(ProductVo productVo){
        propertyService.save(productVo);
        return JsonResultVO.success();
    }

}
